package org.javaforever.gatescore.widgets;

import java.util.ArrayList;
import java.util.List;

import org.javaforever.gatescore.core.FrontDomain;
import org.javaforever.gatescore.core.FrontDropdown;
import org.javaforever.gatescore.core.FrontField;
import org.javaforever.gatescore.core.Statement;
import org.javaforever.gatescore.core.StatementList;
import org.javaforever.gatescore.core.Writeable;
import org.javaforever.gatescore.core.WriteableUtil;
import org.javaforever.gatescore.verb.Update;

public class UpdateDialog extends Widget{
	protected FrontDomain domain;
	protected String indexSuffix = "";
	
	public UpdateDialog(FrontDomain domain,String indexSuffix) {
		super();
		this.domain = domain;
		this.indexSuffix = indexSuffix;
	}
	
	@Override
	public StatementList generateWidgetStatements() {
		try {
			List<Writeable> sList = new ArrayList<>();
			if (!this.domain.isVerbDenied("Update")&&!(this.domain instanceof org.javaforever.gatescore.core.FrontEnum)) {
				sList.add(new Statement(serial+8000L,0,"<el-dialog"));
				sList.add(new Statement(serial+9000L,1,":before-close=\"handleClose\""));
				sList.add(new Statement(serial+10000L,1,":visible.sync=\"editDialogVisible"+this.indexSuffix+"\""));
				if (this.domain.getLanguage().equalsIgnoreCase("english")) {
					sList.add(new Statement(serial+11000L,1,"title=\"Update\""));
				}else {
					sList.add(new Statement(serial+11000L,1,"title=\"编辑\""));
				}
				sList.add(new Statement(serial+12000L,1,"width=\"850px\">"));
				sList.add(new Statement(serial+13000L,1,"<el-form :model=\"form"+this.indexSuffix+"\" label-position=\"right\" label-width=\"120px\">"));
				sList.add(new Statement(serial+14000L,2,"<el-input v-model=\"form"+this.indexSuffix+"."+this.domain.getDomainId().getLowerFirstFieldName()+"\" type=\"hidden\"/>"));
				sList.add(new Statement(serial+15000L,2,"<el-row>"));
				serial = serial + 16000L;
				for (FrontField f:this.domain.getAllFieldsWithoutId()) {
					if (!(f instanceof FrontDropdown)) {
						if (f.getFieldType().getTypeName().equalsIgnoreCase("image")) {
							sList.add(new Statement(serial+1000L,3,"<el-col :span=\"12\">"));
							sList.add(new Statement(serial+2000L,4,"<el-form-item label=\""+f.getText()+"\">"));
							sList.add(new Statement(serial+3000L,5,"<el-upload"));
							sList.add(new Statement(serial+3600L,6,":headers=\"this.token\""));
							sList.add(new Statement(serial+4000L,6,":action=\"this.baseApi+'"+this.domain.getLowerFirstDomainName()+this.domain.getControllerNamingSuffix()+"/addUpload"+this.domain.getCapFirstDomainName()+f.getCapFirstFieldName()+"'\""));
							sList.add(new Statement(serial+5000L,6,":show-file-list=\"false\""));
							sList.add(new Statement(serial+6000L,6,":on-success=\"handle"+this.domain.getCapFirstDomainName()+f.getCapFirstFieldName()+"ImageSuccess\""));
							sList.add(new Statement(serial+7000L,6,":before-upload=\"before"+this.domain.getCapFirstDomainName()+f.getCapFirstFieldName()+"ImageUpload\""));
							sList.add(new Statement(serial+7500L,6,"name=\"files[]\">"));
							sList.add(new Statement(serial+8000L,6,"<img v-if=\""+f.getLowerFirstFieldName()+"ImageUrl\" :src=\""+f.getLowerFirstFieldName()+"ImageUrl\" width=\"300\">"));
							sList.add(new Statement(serial+8500L,6,"<img v-else-if=\"!form"+this.indexSuffix+"."+f.getLowerFirstFieldName()+"\" :src=\"'../static/images/blank.jpg'\" width=\"300\">"));
							sList.add(new Statement(serial+9000L,6,"<img v-else :src=\"'data:image/png;base64,'+form"+this.indexSuffix+"."+f.getLowerFirstFieldName()+"\" width=\"300\">"));
							sList.add(new Statement(serial+10000L,6,"<br>"));
							if (this.domain.getLanguage().equalsIgnoreCase("english")) {
								sList.add(new Statement(serial+11000L,6,"<el-button type=\"primary\" plain @click=\"up"+this.domain.getCapFirstDomainName()+f.getCapFirstFieldName()+"ImageData\">Upload</el-button>"));
							}else {  
								sList.add(new Statement(serial+11000L,6,"<el-button type=\"primary\" plain @click=\"up"+this.domain.getCapFirstDomainName()+f.getCapFirstFieldName()+"ImageData\">上传</el-button>"));
							}
							sList.add(new Statement(serial+12000L,5,"</el-upload>"));
							sList.add(new Statement(serial+13000L,4,"</el-form-item>"));
							sList.add(new Statement(serial+14000L,3,"</el-col>"));
						}else if (f.getFieldType().getTypeName().equalsIgnoreCase("boolean")){
							sList.add(new Statement(serial,3,"<el-col :span=\"12\">"));
							sList.add(new Statement(serial+1000L,4,"<el-form-item label=\""+f.getText()+"\">"));
							sList.add(new Statement(serial+2000L,5,"<el-input v-model=\"form"+this.indexSuffix+"."+f.getFieldName()+"\" style=\"width: 240px;\"/>"));
							sList.add(new Statement(serial+3000L,4,"</el-form-item>"));
							sList.add(new Statement(serial+4000L,3,"</el-col>"));
						}else if (f.getFieldType().getTypeName().equalsIgnoreCase("datetime")) {
							sList.add(new Statement(serial+100L,3,"<el-col :span=\"12\">"));
							sList.add(new Statement(serial+200L,4,"<el-form-item label=\""+f.getText()+"\">"));
							sList.add(new Statement(serial+1000L,5,"<el-date-picker"));
							sList.add(new Statement(serial+2000L,6,"value-format=\"yyyy-MM-dd HH:mm:ss\""));
							sList.add(new Statement(serial+3000L,6,"v-model=\"form"+this.indexSuffix+"."+f.getLowerFirstFieldName()+"\""));
							sList.add(new Statement(serial+4000L,6,"type=\"datetime\""));
							sList.add(new Statement(serial+5000L,6,"style=\"width: 160px\""));
							sList.add(new Statement(serial+6000L,6,"placeholder=\"选择"+f.getText()+"\""));
							sList.add(new Statement(serial+7000L,6,"label=\""+f.getText()+"\""));
							sList.add(new Statement(serial+8000L,5,"/>"));
							sList.add(new Statement(serial+9100L,4,"</el-form-item>"));
							sList.add(new Statement(serial+9200L,3,"</el-col>"));
						} else if (f.getFieldType().getTypeName().equalsIgnoreCase("date")) {
							sList.add(new Statement(serial+100L,3,"<el-col :span=\"12\">"));
							sList.add(new Statement(serial+200L,4,"<el-form-item label=\""+f.getText()+"\">"));
							sList.add(new Statement(serial+1000L,5,"<el-date-picker"));
							sList.add(new Statement(serial+2000L,6,"value-format=\"yyyy-MM-dd\""));
							sList.add(new Statement(serial+3000L,6,"v-model=\"form"+this.indexSuffix+"."+f.getLowerFirstFieldName()+"\""));
							sList.add(new Statement(serial+4000L,6,"type=\"date\""));
							sList.add(new Statement(serial+5000L,6,"style=\"width: 160px\""));
							sList.add(new Statement(serial+6000L,6,"placeholder=\"选择"+f.getText()+"\""));
							sList.add(new Statement(serial+7000L,4,"label=\""+f.getText()+"\""));
							sList.add(new Statement(serial+8000L,5,"/>"));
							sList.add(new Statement(serial+9100L,4,"</el-form-item>"));
							sList.add(new Statement(serial+9200L,3,"</el-col>"));
						}else {
							sList.add(new Statement(serial,3,"<el-col :span=\"12\">"));
							sList.add(new Statement(serial+1000L,4,"<el-form-item label=\""+f.getText()+"\">"));
							sList.add(new Statement(serial+2000L,5,"<el-input v-model=\"form"+this.indexSuffix+"."+f.getFieldName()+"\" style=\"width: 240px;\"/>"));
							sList.add(new Statement(serial+3000L,4,"</el-form-item>"));
							sList.add(new Statement(serial+4000L,3,"</el-col>"));
						}
						serial = serial + 15000L;
					}else {
						FrontDropdown dp = (FrontDropdown)f;
						sList.add(new Statement(serial,3,"<el-col :span=\"12\">"));
						sList.add(new Statement(serial+1000L,4,"<el-form-item label=\""+dp.getText()+"\" prop=\""+dp.getAliasOrName()+"\">"));
						if (this.domain.getLanguage().equalsIgnoreCase("english")) {
							sList.add(new Statement(serial+2000L,5,"<el-select v-model=\"form"+this.indexSuffix+"."+dp.getAliasName()+"\" value-key=\""+dp.getTarget().getDomainId().getFieldName()+"\" placeholder=\"Please choose \" style=\"width: 240px;\" @change=\"translate"+dp.getTarget().getCapFirstDomainName()+"\">"));
						}else {
							sList.add(new Statement(serial+2000L,5,"<el-select v-model=\"form"+this.indexSuffix+"."+dp.getAliasName()+"\" value-key=\""+dp.getTarget().getDomainId().getFieldName()+"\" placeholder=\"请选择\" style=\"width: 240px;\" @change=\"translate"+dp.getTarget().getCapFirstDomainName()+"\">"));
						}
						sList.add(new Statement(serial+2500L,6,"<el-option key=\""+dp.getAliasName()+"\" :value=\"-1\" label=\"---\" />"));
						sList.add(new Statement(serial+3000L,6,"<el-option"));
						sList.add(new Statement(serial+4000L,7,"v-for=\"item in active"+dp.getTarget().getCapFirstPlural()+"\""));
						sList.add(new Statement(serial+5000L,7,":key=\"item."+dp.getAliasName()+"\""));
						sList.add(new Statement(serial+6000L,7,":label=\"item."+dp.getTarget().getDomainName().getFieldName()+"\""));
						sList.add(new Statement(serial+7000L,7,":value=\"item."+dp.getTarget().getDomainId().getFieldName()+"\"/>"));
						sList.add(new Statement(serial+9000L,5,"</el-select>"));
						sList.add(new Statement(serial+10000L,4,"</el-form-item>"));
						sList.add(new Statement(serial+11000L,3,"</el-col>"));
						serial = serial + 15000L;
					}
				}
				sList.add(new Statement(serial,2,"</el-row>"));
				sList.add(new Statement(serial+1000L,1,"</el-form>"));
				sList.add(new Statement(serial+2000L,1,"<span slot=\"footer\" class=\"dialog-footer\">"));
				if (this.domain.getLanguage().equalsIgnoreCase("english")) {
					sList.add(new Statement(serial+3000L,2,"<el-button @click=\"editDialogVisible"+this.indexSuffix+" = false\">Cancel</el-button>"));
					sList.add(new Statement(serial+4000L,2,"<el-button type=\"primary\" @click=\"update"+this.domain.getCapFirstDomainName()+"\">Update</el-button>"));
				}else {
					sList.add(new Statement(serial+3000L,2,"<el-button @click=\"editDialogVisible"+this.indexSuffix+" = false\">取 消</el-button>"));
					sList.add(new Statement(serial+4000L,2,"<el-button type=\"primary\" @click=\"update"+this.domain.getCapFirstDomainName()+"\">编辑</el-button>"));
				}
				sList.add(new Statement(serial+5000L,1,"</span>"));
				sList.add(new Statement(serial+6000L,0,"</el-dialog>"));
			}
		
			StatementList sl = WriteableUtil.merge(sList);
			sl.setSerial(this.serial);
			return sl;
		}catch (Exception e) {
			e.printStackTrace();
			return null;
		}		
	}

	@Override
	public StatementList generateWidgetScriptStatements() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean parse() {
		// TODO Auto-generated method stub
		return false;
	}

	public FrontDomain getDomain() {
		return domain;
	}

	public void setDomain(FrontDomain domain) {
		this.domain = domain;
	}

	public String getIndexSuffix() {
		return indexSuffix;
	}

	public void setIndexSuffix(String indexSuffix) {
		this.indexSuffix = indexSuffix;
	}
}
